<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml"> 
<head> 
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 
<script type="text/javascript" src="<?php echo base_url()?>js/jquery-1.6.1.min.js"></script>
<script type="text/javascript" src="<?php echo base_url()?>js/core.js"></script>
<script type="text/javascript" src="<?php echo base_url()?>js/jquery.md5.js"></script>
<script type="text/javascript" >
    jQuery(document).ready(function(){
        var baseUrl = "<?php echo base_url() ?>"
        $("#register").live("click",function(){
            var mess = "";
            var array = [];
            $("#error_message").empty();
            var username = $("#username").val();
            if(username.length == 0){
                mess = "Phải nhập tên tài khoản";
                $("#error_message").text(mess);
                return ;
            }
            array.push("username="+username);
            
            var password = $("#password").val();
            var rePassword = $("#re_password").val();
            if(password.length == 0){
                mess ="Phải nhập mật khẩu";
                $("#error_message").text(mess);
                return ;
            }
            if(password != rePassword){
                mess ="Mật khẩu phải giống nhau";
                $("#error_message").text(mess);
                return;
            }
            password = $.md5(password);
            array.push("&password="+password);
            
            myregexp = /^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$/; 
            var email = $("#email").val();
            if(email.length == 0){
                mess = "Phải nhập email";
                $("#error_message").text(mess);
                return ;
            }
            var isMatch = myregexp.test(email);
            if(!isMatch) {  
                mess = "Email không đúng định dạng";
                $("#error_message").text(mess);
                return ;
            }
            array.push("&email="+email);
            
            var gender = $("input[type='radio']:checked").val();
            array.push("&gender="+gender);
            
            var country = $("#country").val();
            array.push("&country="+country);
            
            var province = $("#province").val();
            array.push("&province="+province); 
            
            var city = $("#city").val();                 
            array.push("&city="+city);
            
            $.ajax({
                type: "POST",
                url:baseUrl+"register/register_user/",
                data: array.join(""),
                dataType: "json",
                async: false,
                beforeSend : function(){
                    $("#error_message").html("LOADDING....");
                },
                success: function(json) {
                    status = json.status;
                    message = json.message;
                    alert(message);
                    if(status == "true"){
                        document.location = baseUrl+"login";
                    }else{
                        $("#error_message").html(message);
                    }
                }
            })
        })
    })
</script>
<title>Untitled Document</title> 
</head> 
    <body>
        <label for="username">Username:</label>
        <input type="text" size="20" id="username" name="username"/>
        <br/>
        
        <label for="password">Password:</label>
        <input type="password" size="20" id="password" name="password"/>
        <br/>
        
        <label for="password">Re-enter password:</label>
        <input type="password" size="20" id="re_password" name="re_password"/>
        <br/>
        
        <label>Email:</label>
        <input type="text" size="50" id="email" name="email"/>
        <br/>
        
        <label>Gender:</label>
        <input type="radio" name="gender" checked value="male">Male
        <input type="radio" name="gender" value="female">Female
        <br/>
        
        <label>County:</label>
        <select id="country" name ="country">
            <option value ="Việt Nam">Việt Nam</option>
            <option value ="Tàu Khựa">Tàu Khựa</option>
            <option value ="Hoa Kỳ">Hoa Kỳ</option>
        </select>
        <br/>
        
        <label>Province:</label>
        <select id="province" name ="province">
            <option value ="Hà Nội">Hà Nội</option>
            <option value ="Hồ Chí Minh">Hồ Chí Minh</option>
            <option value ="Đà Nẵng">Đà Nẵng</option>
        </select>
        <br/>
        
        <label>City:</label>
        <input type="text" size="50" id="city" name="city"/>
        <br/>
        
        <label id="error_message" style="color: red"></label><br/>
        
        <input type="button" value="Register" id ="register"/>
        
    </body> 
</html>